from code_builder.tools import read_mysql, utils


def create_code_model(data):
    for x in data:
        if x[0] != "status" and x[0] != "create_time" and x[0] != "update_time":
            if x[1].find("char") != -1:
                print("private String " + utils.to_low_camel_case(x[0]) + ";")
            elif x[1].find("int") != -1:
                print("private Integer " + utils.to_low_camel_case(x[0]) + ";")
            elif x[1].find("datetime") != -1:
                print("@DateTimeFormat(pattern = \"yyyy-MM-dd\")")
                print("private Date " + utils.to_low_camel_case(x[0]) + ";")


def create_code_insert(data, table_name):
    for x in data:
        # print(x)
        if x[0] != "status" and x[0] != "create_time" and x[0] != "update_time":
            if x[2].find("NO") != 0:
                if x[1].find("char") != -1:
                    print("if (StringUtils.isNotBlank(" + utils.to_low_camel_case(
                        table_name) + ".get" + utils.to_cap_camel_case(x[0]) + "())) {")
                elif x[1].find("int") != -1:
                    print("if (" + utils.to_low_camel_case(table_name) + ".get" + utils.to_cap_camel_case(
                        x[0]) + "() != null) {")
                elif x[1].find("time") != -1:
                    print("if (" + utils.to_low_camel_case(table_name) + ".get" + utils.to_cap_camel_case(
                        x[0]) + "() != null) {")
            print("VALUES(\"" + x[0] + "\", \"#{" + utils.to_low_camel_case(x[0]) + "}\");")
            if x[2].find("NO") != 0:
                print("}")


def create_code_update(data, table_name):
    for x in data:
        # print(x)
        if x[0] != "status" and x[0] != "create_time" and x[0] != "update_time" and x[0] != "id" and x[
            0] != "person_id":
            # if x[1].find("char") != -1:
            # print("if (StringUtils.isNotBlank(" + utils.to_low_camel_case(
            #     table_name) + ".get" + utils.to_cap_camel_case(x[0]) + "())) {")
            #     print("if (" + utils.to_low_camel_case(table_name) + ".get" + utils.to_cap_camel_case(
            #         x[0]) + "() != null) {")
            # elif x[1].find("int") != -1:
            #     print("if (" + utils.to_low_camel_case(table_name) + ".get" + utils.to_cap_camel_case(
            #         x[0]) + "() != null) {")
            # elif x[1].find("time") != -1:
            #     print("if (" + utils.to_low_camel_case(table_name) + ".get" + utils.to_cap_camel_case(
            #         x[0]) + "() != null) {")
            print("SET(\"" + x[0] + "=#{" + utils.to_low_camel_case(x[0]) + "}\");")
            # print("}")


if __name__ == "__main__":
    # table_name = "person_correct"
    table_name_list_person = ["person_base", "person_household", "person_floating", "person_countryside",
                              "person_overseas","person_permanent"]
    table_name_list_campus = ["school", "school_case", "school_near_people"]
    table_name_list_route = ["route", "route_case","route_case_middle"]
    table_name_list_key = ["person_released_prisoner", "person_correct", "person_drug", "person_psychosis",
                           "person_aids","person_cult","person_army","person_petition"]
    table_name_list_family_planning = ["contraception", "gynae_check", "three_bond"]
    table_name_list_focused_youth = ["focused_youth"]
    table_name_list_log = ["operation_log","login_log"]
    table_name_list_all = table_name_list_person + table_name_list_campus + table_name_list_key + table_name_list_family_planning + table_name_list_focused_youth
    table_name_list_one = ["notice"]
    table_name_list = table_name_list_one
    data = read_mysql.desc_table_list(table_name_list)
    for i in range(0, len(data)):
        print("-" * 13 + table_name_list[i] + "-" * 13)
        print("-" * 8 + "model" + "-" * 8)
        create_code_model(data[i])
        print("-" * 8 + "insert" + "-" * 8)
        create_code_insert(data[i], table_name_list[i])
        print("-" * 8 + "update" + "-" * 8)
        create_code_update(data[i], table_name_list[i])
